package com.android.vgongyi.activity;

import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.vgongyi.R;
import com.android.vgongyi.entity.FeedbackEntity;
import com.android.vgongyi.service.VGongyiService;
import com.android.vgongyi.utils.SystemUtil;
/**
 * 快速回复
 * @author Ryan
 *
 */
public class SugesstionActivity extends BaseActivity implements OnClickListener{
	private Button   backBtn;
	private EditText sugesstionContentEditText;
	private TextView textCountTextView;
	private TextView contackWayTextView;
	private Button   confirmButton;
	
	public final String SUC                = "1";
	public final String ERROR_SUGGESTION   ="ERROR_NO_SUGGESTION";
	public final String ERROR_CONTACT      = "ERROR_NO_CONTACT";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sugesstion);
        setCommonTitle(this, R.string.feedback);
        
        backBtn                    = (Button) this.findViewById(R.id.Common_back);             
        textCountTextView          = (TextView) this.findViewById(R.id.text_count);
        contackWayTextView         = (TextView) this.findViewById(R.id.contact_way);
        confirmButton              = (Button) this.findViewById(R.id.btn_suggestion);
        sugesstionContentEditText  = (EditText) this.findViewById(R.id.sugesstion_content);
        
        confirmButton.setOnClickListener(this);
        backBtn.setVisibility(View.VISIBLE);
       
        
        sugesstionContentEditText.addTextChangedListener(new TextWatcher() {
			
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				textCountTextView.setText(String.valueOf(140 - sugesstionContentEditText.getText().length()));
				
			}
			
			public void beforeTextChanged(CharSequence s, int start, int count,int after) {}
			
			public void afterTextChanged(Editable s) {}
			
		});
        
        backBtn.setOnClickListener(this);
        
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.Common_back:

			SugesstionActivity.this.finish();
			break;
		case R.id.btn_suggestion://提交意见
			
			String contact    = contackWayTextView.getText().toString().trim();
			String suggestion = sugesstionContentEditText.getText().toString().trim();							
			try {
				postFeedback(contact,suggestion);
			} catch (NameNotFoundException e) {
				e.printStackTrace();
			}
						
			break;
		}
	}
	
	
	public void postFeedback(final String contact,final String suggestion) throws NameNotFoundException{
						
			if(!SystemUtil.checkNetwork(SugesstionActivity.this)){
				
	    			Toast.makeText(SugesstionActivity.this, R.string.network_unavailable, Toast.LENGTH_SHORT).show();
	    			
	    	}else{
	    		
	    			VGongyiService service        = new VGongyiService(getApplicationContext());
					FeedbackEntity feedbackEntity = service.postFeedback(contact, suggestion);				
					if(feedbackEntity != null){		
						
						if(feedbackEntity.getResult().equals(SUC)){
							
							Toast.makeText(SugesstionActivity.this, R.string.thank_feedback, Toast.LENGTH_SHORT).show();
							SugesstionActivity.this.finish();
							
						}else{
							
						    if(feedbackEntity.getMsg().equals(ERROR_CONTACT)){
						    	
						    	Toast.makeText(SugesstionActivity.this, R.string.feedback_error_contact, Toast.LENGTH_SHORT).show();
						    	
						    }
						    else if(feedbackEntity.getMsg().equals(ERROR_SUGGESTION)){
						    	
						    	Toast.makeText(SugesstionActivity.this, R.string.feedback_error_suggestion, Toast.LENGTH_SHORT).show();
						    	
						    }
						}
						
					}else{
						
						Toast.makeText(SugesstionActivity.this, R.string.network_unavailable, Toast.LENGTH_SHORT).show();
						
					}
	    		}						
			}
				
}
	